<template>
  <el-row class="logon-big">
    <el-row class="logon-box">
      <el-col :span="12" class="logon-box">
        <el-col :span="12">
          <img src="../../public/img/icons/logon.png" alt="">
        </el-col>
        <el-col :span="12">
          <el-col :span="4">

          </el-col>
          <el-col :span="16" class="center-container">
            <h2>后台管理系统</h2>
            <p>MANAGEMENT SYSTEM</p>
            <div class="username-input">
              <el-input placeholder="用户名" v-model="form.username"></el-input>
              <p>{{ usernameMsg }}</p>
            </div>
            <div class="password-input">
              <el-input placeholder="密码" v-model="form.password"></el-input>
              <p>{{ passwordMsg }}</p>
            </div>
            <div>
              <!-- 深蓝色 -->
              <el-button type="primary" @click="handleLogin" style="width: 208px;">登录</el-button>
            </div>
          </el-col>
          <el-col :span="4">
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </el-row>
</template>

<script>
import http from '@/mixins/http'

export default {
  mixins: [http()],

  data: () => ({
    form: {
      username: '',
      password: ''
    },
    loading: false
  }),

  methods: {
    async handleLogin() {
      try {
        this.loading = true

        const { data: { data, meta } } = await this.$httpPost('/login', this.form)

        if (meta.status !== 200) {
          this.$message.error(meta.msg || '登录失败')
          return
        }

        localStorage.setItem('token', data.token)
        this.$router.push({ name: 'list' })
        this.$message.success('登录成功')

      } catch (error) {
        this.$handleError({
          error,
          defaultMessage: '登录请求失败',
          timeout: 3000
        })
      } finally {
        this.loading = false
      }
    }
  }
}
</script>
